//
//  LTHeadLineViewController.m
//  wangyiNews08
//
//  Created by 123 on 16/3/11.
//  Copyright © 2016年 Tsr. All rights reserved.
//

#import "LTHeadLineViewController.h"
#import "LTNetwrokTool.h"
#import "LTHeadLine.h"
#import "LTHeadLineCell.h"

// 返回的组数
#define LTGroupCount 10000

@interface LTHeadLineViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *flowLayout;
@property (nonatomic, strong) NSArray *headLines;
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
// 标题
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
// 分页
@property (weak, nonatomic) IBOutlet UIPageControl *pageControl;
@end

@implementation LTHeadLineViewController

static NSString * const reuseIdentifier = @"headLine";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 设置数据源,代理
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    
    [self loadData];
}


// 当控制器的view加载完后,调用这个方法
- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    // 设置布局
    [self setupLayout];
}

/// 加载数据
- (void) loadData {
    [LTHeadLine headLineWithSuccess:^(NSArray *headLines) {
        
        self.headLines = headLines;
        
        [self.collectionView reloadData];
        
        // 默认显示中间组
        [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:LTGroupCount * 0.5] atScrollPosition:UICollectionViewScrollPositionCenteredVertically animated:NO];
        
        // 设置标题
        [self scrollViewDidScroll:self.collectionView];
    } error:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}

/// 流水布局
- (void) setupLayout {
    
    self.collectionView.backgroundColor = [UIColor whiteColor];
    
    self.flowLayout.itemSize = self.collectionView.bounds.size;
    self.flowLayout.minimumLineSpacing = 0;
    self.flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.collectionView.pagingEnabled = YES;
    self.collectionView.bounces = NO;
    self.collectionView.showsHorizontalScrollIndicator = NO;
}


#pragma mark <UICollectionViewDataSource>
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return LTGroupCount;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.headLines.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    LTHeadLineCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    
    LTHeadLine *headLine = self.headLines[indexPath.item];
    
    cell.headLine = headLine;
    
    return cell;
}

#pragma mark - scrollView的代理方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    int page = (scrollView.contentOffset.x + (scrollView.bounds.size.width * 0.5))/ scrollView.bounds.size.width;
    page %= self.headLines.count;
    
    // 获取模型
    LTHeadLine *headLine = self.headLines[page];
    self.pageControl.currentPage = page;
    self.titleLabel.text = headLine.title;
}
@end
